package com.celeritymq.session;

import java.lang.reflect.InvocationTargetException;
import java.net.SocketAddress;

import javax.jms.JMSException;
import javax.jms.ServerSession;
import javax.jms.ServerSessionPool;

import com.celeritymq.utils.CelerityConstructorParameter;
import com.celeritymq.utils.CelerityPool;
import com.celeritymq.utils.CelerityPoolImpl;

public class CelerityServerSessionPool implements ServerSessionPool {
	private CelerityPool<CelerityServerSession> pool;

	public CelerityServerSessionPool(SocketAddress clientAddress, int bufferSize) throws SecurityException,
			IllegalArgumentException, NoSuchMethodException, InstantiationException, IllegalAccessException,
			InvocationTargetException, InterruptedException {
		CelerityConstructorParameter<CelerityServerSession> parameter = new CelerityConstructorParameter<CelerityServerSession>(
				CelerityServerSession.class, new Class[] { SocketAddress.class, Integer.class }, new Object[] { clientAddress,
						bufferSize });
		pool = new CelerityPoolImpl<CelerityServerSession>(parameter, 10);
	}

	@Override
	public ServerSession getServerSession() throws JMSException {
		try {
			return pool.get();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public void releaseServerSession() {
		pool.release();
	}

	public void close() {
		pool.close();
		pool = null;
	}

}
